﻿using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Diagnostics;
using System.IO;

namespace 扫雷辅助
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }

        #region DLLImport
        [DllImport("user32.dll")]
        static extern IntPtr FindWindow(string strClass, string strWindow);
        [DllImport("user32.dll")]
        private static extern int GetWindowRect(IntPtr hwnd, out RECT lpRect);
        [DllImport("user32.dll")]
        static extern int SetCursorPos(int x, int y);
        [DllImport("user32.dll")]
        static extern int SetForegroundWindow(IntPtr hwnd);
        [DllImport("user32.dll")]
        private static extern int mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
        [DllImport("user32.dll")]
        private static extern void SetWindowPos(IntPtr hwnd, int hWndInsertAfter, int x, int y, int cx, int cy, int wFlags);
        [DllImport("user32.dll")]
        public extern static bool GetCursorPos(ref Point lpPoint);
        [DllImport("user32.dll")]
        public extern static bool GetWindowThreadProcessId(IntPtr hWnd, ref int processId);
        [DllImport("kernel32.dll")]
        public static extern int OpenProcess(int dwDesiredAccess, int bInheritHandle, int dwProcessId);
        [DllImport("kernel32.dll")]
        public static extern bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress,
            byte[] lpBuffer, uint nSize, out int lpNumberOfBytesRead);
        [DllImport("user32.dll", EntryPoint = "SendMessage")]
        private static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);

        #endregion

        public struct RECT
        {
            public int Left;
            public int Top;
            public int Right;
            public int Bottom;
        }

        #region MOUSE_EVENT
        IntPtr hwnd;
        const int MOUSEEVENTF_MOVE = 0x0001;
        const int MOUSEEVENTF_LEFTDOWN = 0x0002;
        const int MOUSEEVENTF_LEFTUP = 0x0004;
        const int MOUSEEVENTF_RIGHTDOWN = 0x0008;
        const int MOUSEEVENTF_RIGHTUP = 0x0010;
        const int MOUSEEVENTF_MIDDLEDOWN = 0x0020;
        const int MOUSEEVENTF_MIDDLEUP = 0x0040;
        const int MOUSEEVENTF_ABSOLUTE = 0x8000;
        #endregion

        private void Mouse_LeftClick(int x, int y)
        {

            mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            hwnd = FindWindow("扫雷", "扫雷");
            if (hwnd == IntPtr.Zero)
            {
                MessageBox.Show("没有运行扫雷！", "", MessageBoxButtons.OK);
            }
            else
            {
                RECT hrect = new RECT();
                GetWindowRect(hwnd, out hrect);

                int Streght = hrect.Right - hrect.Left;

                Point p = new Point(1, 1);
                GetCursorPos(ref p);

                SetCursorPos(hrect.Left + Streght / 2, hrect.Top + 81);
                Mouse_LeftClick(hrect.Left + Streght / 2, hrect.Top + 81);
                //Thread.Sleep(50);
                Mouse_LeftClick(hrect.Left + Streght / 2, hrect.Top + 81);

                SetCursorPos(p.X, p.Y);
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            const int PROCESS_ALL_ACCESS = 0x1F0FFF;

            hwnd = FindWindow("扫雷", "扫雷");
            if (hwnd == IntPtr.Zero)
            {
                MessageBox.Show("没有运行扫雷！", "", MessageBoxButtons.OK);
            }
            else
            {

                int lpnumberofbyteswritten = 0;

                int processId = 0;
                SetWindowPos(hwnd, 0, 0, 0, 0, 0, 3);
                GetWindowThreadProcessId(hwnd, ref processId);
                int handleOfProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, processId);

                byte[] width = new byte[2];
                ReadProcessMemory((IntPtr)handleOfProcess, (IntPtr)16798516,
                    width, 1, out lpnumberofbyteswritten);


                byte[] height = new byte[1];
                ReadProcessMemory((IntPtr)handleOfProcess, (IntPtr)16798520,
                    height, 1, out lpnumberofbyteswritten);

                const int y0 = 63;
                const int x0 = 19;
                const int gao = 16;
                const int kuan = 16;

                ///MessageBox.Show("height:" + height[0].ToString() + "  width:" + width[0].ToString());

                for (int y = 1; y <= height[0]; y++)
                {
                    byte[] mem = new byte[width[0]];
                    ReadProcessMemory((IntPtr)handleOfProcess, (IntPtr)(16798561 + (y - 1) * 32),
                        mem, width[0], out lpnumberofbyteswritten);

                    for (int x = 1; x <= width[0]; x++)
                    {
                        if (mem[x - 1] == 15)
                        {
                            SendMessage(hwnd, 513, 0, ((y0 + (y - 1) * gao) << 16) + x0 + (x - 1) * kuan);
                            SendMessage(hwnd, 514, 0, ((y0 + (y - 1) * gao) << 16) + x0 + (x - 1) * kuan);
                        }
                        else if (mem[x - 1] == 14)
                        {
                            SendMessage(hwnd, 516, 0, ((y0 + (y - 1) * gao) << 16) + x0 + (x - 1) * kuan);
                            SendMessage(hwnd, 517, 0, ((y0 + (y - 1) * gao) << 16) + x0 + (x - 1) * kuan);

                            SendMessage(hwnd, 516, 0, ((y0 + (y - 1) * gao) << 16) + x0 + (x - 1) * kuan);
                            SendMessage(hwnd, 517, 0, ((y0 + (y - 1) * gao) << 16) + x0 + (x - 1) * kuan);

                            SendMessage(hwnd, 513, 0, ((y0 + (y - 1) * gao) << 16) + x0 + (x - 1) * kuan);
                            SendMessage(hwnd, 514, 0, ((y0 + (y - 1) * gao) << 16) + x0 + (x - 1) * kuan);
                        }
                        else if (mem[x - 1] == 13)
                        {
                            SendMessage(hwnd, 516, 0, ((y0 + (y - 1) * gao) << 16) + x0 + (x - 1) * kuan);
                            SendMessage(hwnd, 517, 0, ((y0 + (y - 1) * gao) << 16) + x0 + (x - 1) * kuan);

                            SendMessage(hwnd, 513, 0, ((y0 + (y - 1) * gao) << 16) + x0 + (x - 1) * kuan);
                            SendMessage(hwnd, 514, 0, ((y0 + (y - 1) * gao) << 16) + x0 + (x - 1) * kuan);
                        }
                        else if (mem[x - 1] == 143)
                        {
                            SendMessage(hwnd, 516, 0, ((y0 + (y - 1) * gao) << 16) + x0 + (x - 1) * kuan);
                            SendMessage(hwnd, 517, 0, ((y0 + (y - 1) * gao) << 16) + x0 + (x - 1) * kuan);
                        }
                        else if (mem[x - 1] == 13)
                        {
                            SendMessage(hwnd, 516, 0, ((y0 + (y - 1) * gao) << 16) + x0 + (x - 1) * kuan);
                            SendMessage(hwnd, 517, 0, ((y0 + (y - 1) * gao) << 16) + x0 + (x - 1) * kuan);

                            SendMessage(hwnd, 516, 0, ((y0 + (y - 1) * gao) << 16) + x0 + (x - 1) * kuan);
                            SendMessage(hwnd, 517, 0, ((y0 + (y - 1) * gao) << 16) + x0 + (x - 1) * kuan);
                        }

                    }

                }


            }


        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (File.Exists("winmine.exe"))
            {
                Process.Start("winmine.exe");
            }
            else
            {
                MessageBox.Show("没有发现当前目录下存在扫雷应用程序winmine.exe！");
            }
        }

    }
}
